package com.wgts.wgts.mvp.view.activity.shopkeeper;

import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentStatePagerAdapter;
import android.support.v4.content.ContextCompat;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.wgts.wgts.R;
import com.wgts.wgts.base.BaseActivity;
import com.wgts.wgts.control.viewpage.NoScrollViewPager;
import com.wgts.wgts.localbean.LocalShopKeepFansEntity;
import com.wgts.wgts.tools.AppInfor;
import com.wgts.wgts.tools.MoHuGlide;
import com.scwang.smartrefresh.layout.SmartRefreshLayout;
import com.scwang.smartrefresh.layout.api.RefreshLayout;
import com.scwang.smartrefresh.layout.listener.OnLoadmoreListener;

import java.util.ArrayList;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import de.hdodenhof.circleimageview.CircleImageView;

public class MyExtensionFansActivity extends BaseActivity implements MyExtensionFansFragment.MyExtensionFansFragmentToActivity, MyExtensionFansOrderFragment.MyExtensionFansOrderFragmentToActivity {

    public static final String USER_INFO = "USER_INFO";

    @BindView(R.id.img_circle_head)
    CircleImageView imgCircleHead;
    @BindView(R.id.tv_user_name)
    TextView tvUserName;
    @BindView(R.id.img_vip_statue)
    ImageView imgVipStatue;
    @BindView(R.id.tv_vip_state)
    TextView tvVipState;
    @BindView(R.id.linear_vip)
    LinearLayout linearVip;
    @BindView(R.id.img_share_code)
    ImageView imgShareCode;
    @BindView(R.id.tv_code_title)
    TextView tvCodeTitle;
    @BindView(R.id.tv_code)
    TextView tvCode;
    @BindView(R.id.gif_webView)
    ImageView gifWebView;
    @BindView(R.id.tv_fans)
    TextView tvFans;
    @BindView(R.id.tv_nomal_fans)
    TextView tvNomalFans;
    @BindView(R.id.view_page)
    NoScrollViewPager viewPage;
    @BindView(R.id.img_back)
    ImageView imgBack;
    @BindView(R.id.yv_title)
    TextView yvTitle;
    @BindView(R.id.linear_gif_webView)
    LinearLayout linearGifWebView;

    public static final String FANS_STATE = "FANS_STATE";

    /**
     * 1.是直属粉丝
     * 2.是三级粉丝
     */
    public int fansState;
    @BindView(R.id.linear_title)
    LinearLayout linearTitle;
    @BindView(R.id.smart_refresh_layout)
    SmartRefreshLayout smartRefreshLayout;


    private ArrayList<Fragment> fragments;


    private QrCodeDialogFragment fragment;

    private LocalShopKeepFansEntity localShopKeepFansEntity;

    @Override
    public void init() {
        localShopKeepFansEntity = getIntent().getParcelableExtra(USER_INFO);
        fansState = getIntent().getIntExtra(FANS_STATE, 0);
    }

    @Override
    public void onCreate(Bundle saveInstanceBundle) {
        super.onCreate(saveInstanceBundle);
        setContentView(R.layout.my_fans_extension_activity);
        ButterKnife.bind(this);
        initView();
    }

    private void initView() {


        switch (Integer.valueOf(localShopKeepFansEntity.userRole)) {
            case 0:
                imgVipStatue.setImageResource(R.mipmap.ic_store_member_white);

                break;
            case 1:
                imgVipStatue.setImageResource(R.mipmap.ic_store_member_white);

                break;
            case 2:
                imgVipStatue.setImageResource(R.mipmap.ic_store_store_white);

                break;
            case 3:
                imgVipStatue.setImageResource(R.mipmap.ic_data_manager_white);

                break;
            case 4:
                imgVipStatue.setImageResource(R.mipmap.ic_data_rudder_white);

                break;
        }

        smartRefreshLayout.setEnableLoadmore(true);
        smartRefreshLayout.setEnableRefresh(false);

        smartRefreshLayout.setOnLoadmoreListener(new OnLoadmoreListener() {
            @Override
            public void onLoadmore(RefreshLayout refreshlayout) {
                if(fragments.get(viewPage.getCurrentItem()) instanceof MyExtensionFansFragment){
                    ((MyExtensionFansFragment)fragments.get(viewPage.getCurrentItem())).loadMore();
                }
                if(fragments.get(viewPage.getCurrentItem()) instanceof MyExtensionFansOrderFragment){
                    ((MyExtensionFansOrderFragment)fragments.get(viewPage.getCurrentItem())).loadMore();
                }
            }
        });

        tvFans.setText("TA的粉丝");
        tvNomalFans.setText("TA的订单");

        viewPage.setScanScroll(false);


        linearGifWebView.setVisibility(View.GONE);

        if (fansState == 2) {
            fragments = new ArrayList<>();
            if (localShopKeepFansEntity != null) {
                fragments.add(MyExtensionFansFragment.getInstance(0, Integer.valueOf(localShopKeepFansEntity.uesrId), 3));
                fragments.add(MyExtensionFansOrderFragment.getInstance(localShopKeepFansEntity.uesrId));
            }

            viewPage.setAdapter(new FragmentStatePagerAdapter(getSupportFragmentManager()) {
                @Override
                public Fragment getItem(int i) {
                    return fragments.get(i);
                }

                @Override
                public int getCount() {
                    return fragments.size();
                }
            });
        } else {
            linearTitle.setVisibility(View.GONE);
            fragments = new ArrayList<>();
            if (localShopKeepFansEntity != null && Integer.valueOf(localShopKeepFansEntity.userRole) > 1) {
                //fragments.add(MyExtensionFansFragment.getInstance(0, Integer.valueOf(localShopKeepFansEntity.uesrId)));
                fragments.add(MyExtensionFansOrderFragment.getInstance(localShopKeepFansEntity.uesrId));
            }

            viewPage.setAdapter(new FragmentStatePagerAdapter(getSupportFragmentManager()) {
                @Override
                public Fragment getItem(int i) {
                    return fragments.get(i);
                }

                @Override
                public int getCount() {
                    return fragments.size();
                }
            });
        }


        MoHuGlide.getInstance().glideImg(this, localShopKeepFansEntity.userImage, imgCircleHead);
        tvUserName.setText(localShopKeepFansEntity.userNickName);


        if (localShopKeepFansEntity.userRole.equals("0")) {
            tvVipState.setText("内部员工");
        } else if (localShopKeepFansEntity.userRole.equals("1")) {
            tvVipState.setText("普通会员");
        } else if (localShopKeepFansEntity.userRole.equals("2")) {
            tvVipState.setText("店主");
        } else if (localShopKeepFansEntity.userRole.equals("3")) {
            tvVipState.setText("掌柜子");
        } else if (localShopKeepFansEntity.userRole.equals("4")) {
            tvVipState.setText("分舵主");
        }

        //电话号码为获取
        tvCode.setText(localShopKeepFansEntity.userPhone);
    }


    @OnClick({R.id.tv_fans, R.id.tv_nomal_fans, R.id.img_back, R.id.img_share_code})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.tv_fans:
                viewPage.setCurrentItem(0, false);
                tvFans.setBackgroundResource(R.drawable.text_left_bg);
                tvFans.setTextColor(ContextCompat.getColor(this, R.color.whrite));
                tvNomalFans.setTextColor(ContextCompat.getColor(this, R.color.hint_6));
                tvNomalFans.setBackground(null);
                break;
            case R.id.tv_nomal_fans:
                viewPage.setCurrentItem(1, false);
                tvNomalFans.setBackgroundResource(R.drawable.text_right_bg);
                tvFans.setBackground(null);
                tvNomalFans.setTextColor(ContextCompat.getColor(this, R.color.whrite));
                tvFans.setTextColor(ContextCompat.getColor(this, R.color.hint_6));
                break;
            case R.id.img_back:
                finish();
                break;
            case R.id.img_share_code:
                Intent intent = new Intent(MyExtensionFansActivity.this, UpgradeShopKeeperActivity.class);
                startActivity(intent);
                break;
        }
    }


    @Override
    public void onLoadFinish() {
        smartRefreshLayout.finishLoadmore();
    }

    @Override
    public void loadFinish() {
        smartRefreshLayout.finishLoadmore();
    }
}
